package apps.myMagicPiano
{
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.GlowFilter;
    import flash.media.Sound;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.utils.ByteArray;
    import flash.utils.getDefinitionByName;
   [SWF(width="1300",height="700",frameRate="12")]
    public class myMagicPiano extends Sprite
    {
        [Embed(source="assets/pianoSnd.swf",mimeType="application/octet-stream")]
        public var swfAsset:Class;
        private var sndBlocksArr:Array;
        private var indicatorArr:Array;
        private var g1:GlowFilter;
        private var g2:GlowFilter;
        private var ind:int = 0;
        private var assetLoaderInfo:LoaderInfo;
        private var rowsNum:int = 26;
        private var colsNum:int =55;
        
        public function myMagicPiano()
        {
            this.sndBlocksArr = [];
            this.indicatorArr = [];
            this.g1 = new GlowFilter(955419, 1);
            this.g2 = new GlowFilter(16610588, 1);
            super();
            this.loadAsset();
        }
        private function loadAsset():void{
            var loader:* = new Loader();
            var lc:* = new LoaderContext(false, ApplicationDomain.currentDomain);
            loader.loadBytes((new this.swfAsset() as ByteArray), lc);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void{
                initView();
            });
        }
        private function initView():void{
            var block:MovieClip;
            var idc:Sprite;
            var i:int;
            var total:int =rowsNum*colsNum;
            while (i < total) {
                block = new MovieClip();
                block.graphics.beginFill(0xEEEEEE);
                block.graphics.drawRect(0, 0, 20, 20);
                block.graphics.endFill();
                block.x = (((i % colsNum) * 23) + 30);
                block.y = ((uint((i / colsNum)) * 23) + 30);
                block.bOpen = false;
                block.alpha = 0.4;
                block.sndName = ("Piano" + String.fromCharCode(65+uint(i / colsNum)));
                block.addEventListener(MouseEvent.CLICK, this.onClick);
                this.sndBlocksArr.push(block);
                addChild(block);
                i++;
            };
            i = 0;
            while (i < colsNum) {
                idc = new Sprite();
                idc.graphics.beginFill(0xEEEEEE, 0.4);
                idc.graphics.drawRect(0, 0, 20, 20);
                idc.graphics.endFill();
                idc.y = 650;
                idc.x = ((i * 23) + 30);
                addChild(idc);
                this.indicatorArr.push(idc);
                i++;
            };
            addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
        }
        private function onEnterFrame(e:Event):void{
            Sprite(this.indicatorArr[this.ind]).filters = [this.g1];
            if (this.ind == 0){
                Sprite(this.indicatorArr[colsNum-1]).filters = [];
            } else {
                Sprite(this.indicatorArr[(this.ind - 1)]).filters = [];
            };
            var i:int;
            while (i < 26) {
                if (this.sndBlocksArr[(this.ind + (i * colsNum))].bOpen){
                    this.sndBlocksArr[(this.ind + (i * colsNum))].filters = [this.g2];
                    this.playSnd(this.sndBlocksArr[(this.ind + (i * colsNum))].sndName);
                };
                if (this.ind == 0){
                    MovieClip(this.sndBlocksArr[(colsNum-1 + (i * colsNum))]).filters = [];
                } else {
                    MovieClip(this.sndBlocksArr[((this.ind - 1) + (i * colsNum))]).filters = [];
                };
                i++;
            };
            this.ind++;
            this.ind = (this.ind % colsNum);
        }
        private function playSnd(sndName:String):void{
            var snd:Sound = (new ((getDefinitionByName(sndName) as Class))() as Sound);
            snd.play();
        }
        private function onClick(e:MouseEvent):void{
            if (e.target.bOpen){
                e.target.bOpen = false;
                e.target.alpha = 0.4;
            } else {
                e.target.bOpen = true;
                e.target.alpha = 0.8;
            };
        }

    }
}